package com.pinyougou.manager.controller;

import java.util.List;
import java.util.Map;

import entity.PageResult;
import entity.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pinyougou.pojo.TbBrand;
import com.pinyougou.sellergoods.service.BrandService;

@RestController
@RequestMapping("/brand")
public class BrandController {

	@Reference
	private BrandService brandService;

	/**
	 * 查询所有品牌不分页
	 * @return
	 */
	@RequestMapping("/findAll")
	public List<TbBrand> findAll(){
		return brandService.findAll();		
	}


	@RequestMapping("/findPage")
	public PageResult findPage(Integer page,Integer size ){
		return brandService.findPage(page,size);
	}

	/**
	 * 添加品牌
	 * @param tbBrand
	 * @return
	 */
	@RequestMapping("/saveBrand")
	//如果页面对应的是一个对象的字段需要用requstbody进行封装
	public Result saveBrand(@RequestBody TbBrand tbBrand){
		try {

			if (tbBrand!=null && tbBrand.getId()!=null) {
				brandService.update(tbBrand);
			 	return new Result(true,"保存成功...");
			}else{
				brandService.saveBrand(tbBrand);
				return new Result(true,"保存成功...");

			}

		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,"保存失败...");
		}
	}

	/**
	 * 通过id超找品牌对象
	 * @param id
	 * @return
	 */
	@RequestMapping("/findOne")
	public TbBrand findOne(Long id){
		return brandService.findOne(id);
	}


	public Result updata(@RequestBody TbBrand tbBrand){
		try {
			brandService.update(tbBrand);
			return new Result(true,"更新成功...");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,"更新失败...");
		}
	}

	@RequestMapping("/delete")
	public Result delete(Long[] ids){
		try {
			brandService.delete(ids);
			return new Result(true,"删除成功...");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,"更新失败...");
		}
	}

	@RequestMapping("/search")
	public PageResult search(@RequestBody TbBrand tbBrand, Integer page,Integer size ){
		return brandService.findPage(tbBrand,page,size);
	}

    /**
     * 下拉列表数据
     * @return
     */
	@RequestMapping("/selectOptionList")
	public List<Map> selectOptionList(){
		return brandService.selectOptionList();
	}
}
